package com.young.shop.controller;

import com.alibaba.fastjson.JSON;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.BaseRespVOThree;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

//标准信息处理类,处理json数据,其它servlet可继承此类用于json数据处理,并且封装标准信息返回前端
public class BaseController extends HttpServlet {

    //将request中的json数据封装成相应对象
    protected <T> T jsonToObject(HttpServletRequest request,Class<T> tClass){

        StringBuilder jsonString = new StringBuilder();

        try {
            BufferedReader reader = request.getReader();
            String readLine ;
            while ((readLine=reader.readLine())!=null){
                jsonString.append(readLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        T entity = JSON.parseObject(jsonString.toString(), tClass);

        return entity;
    }

    //将标准信息对象转换成json并返回前端
    protected void writerBaseRespVoToResponse(HttpServletResponse response,BaseRespVO baseRespVO){
        String jsonString = JSON.toJSONString(baseRespVO);
        try {
            response.getWriter().write(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected <T> void writerSucccessBaseRespVoToResponse(HttpServletResponse response,T entity){
        BaseRespVO objectBaseRespVO = new BaseRespVO<>();
        //实际上是同一个BaseRespVO对象
        BaseRespVO succcess = objectBaseRespVO.succcess(entity);
        writerBaseRespVoToResponse(response,succcess);
    }
    protected void writerFailedBaseRespVoToResponse(HttpServletResponse response,Exception e){
        BaseRespVO objectBaseRespVO = new BaseRespVO<>();
        String message = e.getMessage();
        BaseRespVO failed = objectBaseRespVO.failed(message);
        writerBaseRespVoToResponse(response,failed);
    }





    //严12/17写入成功or失败的标准信息返回方法
    protected void writerSuccessResult(Object data, HttpServletResponse resp) {
        BaseRespVOThree<Object> result = new BaseRespVOThree<>();
        result.setCode(BaseRespVOThree.CodeType.SUCCESS.getValue());
        result.setData(data);
        String json = JSON.toJSONString(result);
        try {
            resp.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    protected void writerErrorResult(String errorMsg, HttpServletResponse resp) {
        BaseRespVOThree<Object> result = new BaseRespVOThree<>();
        result.setCode(BaseRespVOThree.CodeType.FAILED.getValue());
        result.setMsg(errorMsg);

        String json = JSON.toJSONString(result);
        try {
            resp.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取request对象中的参数，装换成指定类型
    protected <T> T convertParamsToEntity(HttpServletRequest req, Class<T> entityClass) {
        StringBuilder jsonStr = new StringBuilder();
        try {
            BufferedReader reader = req.getReader();
            String tempStr;
            while ((tempStr=reader.readLine())!=null) {
                jsonStr.append(tempStr);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        T entity = JSON.parseObject(jsonStr.toString(), entityClass);
        return entity;
    }
//    返回结果转换为输出流
    protected void writerResult(BaseRespVOThree result, HttpServletResponse resp) {
        String json = JSON.toJSONString(result);
        try {
            resp.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
